﻿using System;
using System.Collections.Generic;
using System.Text;
using System.Data.SqlClient;
using System.Windows.Forms;

namespace EZBarTender_Wrapper.SqlExecutor
{
	public abstract class SqlExecutorBase
	{
		public List<string> Statements { get; private set; }
		public string ConnectionString { get; private set; }
		public abstract void GenerateStatements(string fileName);

		public SqlExecutorBase(string connectionString)
		{
			Statements = new List<string>();
			ConnectionString = connectionString;
		}

		public void Execute()
		{
			foreach (string statement in Statements)
			{
				using (SqlConnection connection = new SqlConnection(ConnectionString))
				{
					connection.Open();
					try
					{
						using (SqlCommand command = new SqlCommand(statement, connection))
						{
							command.ExecuteNonQuery();
						}
					}
					catch (SqlException e)
					{
						MessageBox.Show(
							e.Message + "SQL Statement:\n" + statement,
							"SQL Error",
							MessageBoxButtons.OK,
							MessageBoxIcon.Error);
						//Environment.Exit(1);
					}
					finally
					{
						connection.Close();
					}
				}
			}
		}
	}
}
